package cn.neusoft.service.impl;

import cn.neusoft.domain.Statistics;
import cn.neusoft.mapper.StatisticsMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;

    @Override
    public List<Statistics> getTopFiveCommodityInTimeRange(BaseQuery query) {

        List<Statistics> commodityList = statisticsMapper.getCommodityOrderQuantityInTimeRange(query);

        return commodityList;
    }

    @Override
    public List<Statistics> getCommodityOrderReturnQuantityInTimeRange(BaseQuery query) {
        List<Statistics> commodityList = statisticsMapper.getCommodityOrderReturnQuantityInTimeRange(query);

        return commodityList;
    }

    @Override
    public List<Statistics> getTotalOrderByDay(){
        List<Statistics> list = statisticsMapper.getTotalOrderByDay();

        return list;
    }

    @Override
    public List<Statistics> getTotalOrderByMonth(){
        List<Statistics> list = statisticsMapper.getTotalOrderByMonth();

        return list;
    }

    @Override
    public List<Statistics> getOpByDay(){
        List<Statistics> list = statisticsMapper.getOpByDay();

        return list;
    }

    @Override
    public List<Statistics> getOpByMonth(){
        List<Statistics> list = statisticsMapper.getOpByMonth();

        return list;
    }

    @Override
    public List<Statistics> getOpByAll(){
        List<Statistics> list = statisticsMapper.getOpByAll();

        return list;
    }

    @Override
    public List<Statistics> getStorage(){
        List<Statistics> list = statisticsMapper.getStorage();

        return list;
    }

    @Override
    public List<Map<String, Object>> getOrderByProvince(){
        List<String> list = statisticsMapper.getOrderByProvince();
        String[] stringArray = list.toArray(new String[0]);
        List<String> addressList = statisticsMapper.getOrderByProvince();
        // 手动列出中国行政区划（省、直辖市、自治区）
        List<String> adminDivisions = Arrays.asList(
                "北京", "天津", "上海", "重庆",
                "河北", "山西", "辽宁", "吉林", "黑龙江",
                "江苏", "浙江", "安徽", "福建", "江西", "山东",
                "河南", "湖北", "湖南", "广东", "海南",
                "四川", "贵州", "云南", "陕西", "甘肃", "青海",
                "内蒙古", "广西", "西藏", "宁夏", "新疆"
        );

        // 统计行政区出现次数
        Map<String, Integer> provinceCount = new HashMap<>();
        for (String address : addressList) {
            for (String adminDivision : adminDivisions) {
                if (address.contains(adminDivision)) {
                    provinceCount.put(adminDivision, provinceCount.getOrDefault(adminDivision, 0) + 1);
                    break;
                }
            }
        }

        // 构造处理结果列表
        List<Map<String, Object>> result = new ArrayList<>();
        for (String adminDivision : adminDivisions) {
            int count = provinceCount.getOrDefault(adminDivision, 0);
            Map<String, Object> item = new HashMap<>();
            item.put("name", adminDivision);
            item.put("value", count);
            result.add(item);
        }
        return result;
    };

    @Override
    public String[] getNormalOrderByMonth(){
        List<String> list = statisticsMapper.getNormalOrderByMonth();
        String[] stringArray = list.toArray(new String[0]);

        return stringArray;
    };

    @Override
    public String[] getReturnOrderByMonth(){
        List<String> list = statisticsMapper.getReturnOrderByMonth();
        String[] stringArray = list.toArray(new String[0]);

        return stringArray;
    };

    @Override
    public String[] getExchangeOrderByMonth(){
        List<String> list = statisticsMapper.getExchangeOrderByMonth();
        String[] stringArray = list.toArray(new String[0]);

        return stringArray;
    };
}
